package trans;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;


  @ClassName Dictionary
  @Description TODO
  @Author WSY
  @Date 2020-6-6 0845


public class Dictionary {
    private final String url = jdbcmysqllocalhost3306learnjdbcuseUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT;
    private final String user = root;
    private final String passworad = 123456;

    Dictionary() {
        try {
            Class.forName(com.mysql.cj.jdbc.Driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public String search(String words) {
        String regex = [u4e00-u9fa5];
        String input = 查找失败;
        int count = 0;
        boolean flag = false;
        try (Connection conn = DriverManager.getConnection(url, user, passworad)) {
            List list = new LinkedListString();
            if (words.matches(regex)) {
                try (PreparedStatement ps = conn.prepareStatement(SELECT 汉 FROM dictionary)) {
                    try (ResultSet rs = ps.executeQuery()) {
                        while (rs.next()) {
                            list.add(rs.getString(1));
                        }
                        Iterator it = list.iterator();
                        while (it.hasNext()) {
                            String temp = (String) it.next();
                            StringTokenizer st = new StringTokenizer(temp, ；);
                            while (st.hasMoreTokens()) {
                                if (words.equals(st.nextToken())) {
                                    flag = true;
                                }
                            }
                            if (flag) {
                                break;
                            }
                            count++;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try (PreparedStatement ps = conn.prepareStatement(SELECT  FROM dictionary where 汉 =)) {
                    ps.setObject(1, list.get(count));
                    try (ResultSet rs = ps.executeQuery()) {
                        while (rs.next()) {
                            input = words + n + rs.getString(1);
                        }
                    }
                }
            } else if (words.matches([a-zA-Z])) {
                try (PreparedStatement ps = conn.prepareStatement(SELECT english FROM dictionary)) {
                    try (ResultSet rs = ps.executeQuery()) {
                        while (rs.next()) {
                            list.add(rs.getString(1));
                        }
                        Iterator it = list.iterator();
                        while (it.hasNext()) {
                            String temp = (String) it.next();
                            StringTokenizer st = new StringTokenizer(temp, ;);
                            while (st.hasMoreTokens()) {
                                if (words.equals(st.nextToken())) {
                                    flag = true;
                                }
                            }
                            if (flag) {
                                break;
                            }
                            count++;
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                try (PreparedStatement ps = conn.prepareStatement(SELECT 汉 FROM dictionary where english = )) {
                    ps.setObject(1, list.get(count));
                    try (ResultSet rs = ps.executeQuery()) {
                        while (rs.next()) {
                            input = words + n + rs.getString(1);
                        }
                    }
                }
            } else {
                input = 输入异常!;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return input;
    }

    public void add(String english, String chinese) {
        try (Connection conn = DriverManager.getConnection(url, user, passworad)) {
            try (PreparedStatement ps = conn.prepareStatement(INSERT INTO  dictionary (english,汉) VALUES (,))) {
                ps.setObject(1, english);
                ps.setObject(2, chinese);
                int n = ps.executeUpdate();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}